﻿Public Class Relationship

    Public Enum RelationshipState
        All
        Unknown
        Active
        Terminted
    End Enum

    Private iSponsor As Integer
    Private iChild As Integer
    Private oSponsor As Sponsor
    Private oChild As Child

    Private Sub New()
    End Sub
    Private Sub New(ByVal Id As Integer)
        Load(Id)

    End Sub

    Public ReadOnly Property Sponsor As Sponsor
        Get
            If IsNothing(oSponsor) Then
                oSponsor = Sponsor.GetSponsor(iSponsor)
            End If
            Return oSponsor
        End Get
    End Property
    Public ReadOnly Property Child As Child
        Get
            If IsNothing(oChild) Then
                oChild = Child.GetChild(iChild)
            End If
            Return oChild
        End Get
    End Property

    Public Property StartDate As Date
    Public Property EndDate As Date
    Public Property State As RelationshipState
    Public Property Quota As Single

    Private Sub Load(ByVal Id As Integer)
        Dim dt As New DataTable
        Dim Params As New List(Of IDataParameter)
        Dim Param As IDataParameter
        Dim ret As Integer
        Param = DatabaseLayer.DatabaseManager.InputParameter("RelationshipId", DbType.Int32, Id)
        Params.Add(Param)

        ret = DatabaseLayer.DatabaseManager.StoredProc("uspGetRelationship", Params, dt, User.CurrentUser.Id)
        If ret = 1 Then
            Dim row As DataRow = dt.Rows(0)

            Me.iChild = row("childId")
            Me.iSponsor = row("sponsorId")
            Me.StartDate = DatabaseLayer.DatabaseManager.IsNull(row("startDate"), Date.MinValue)
            Me.EndDate = DatabaseLayer.DatabaseManager.IsNull(row("endDate"), Date.MinValue)
            Me.Quota = DatabaseLayer.DatabaseManager.IsNull(row("quota"), 0)
            Me.State = DatabaseLayer.DatabaseManager.IsNull(row("stateId"), 0)

        Else
            Throw New Exception("relationship " & Id & " not found.")
        End If
    End Sub

    Public Shared Function GetRelationship(ByVal Id As Integer) As Relationship
        Dim oRelationship As Relationship

        oRelationship = New Relationship(Id)

        Return oRelationship

    End Function

    Public Shared Function GetRelationships(ByVal State As RelationshipState,
                                            ByRef dt As DataTable
                                            ) As Integer

        Return GetRelationships(State, 0, 0, dt)

    End Function

    Public Shared Function GetRelationships(ByVal State As RelationshipState,
                                            ByVal PageStart As Integer,
                                            ByVal Rows As Integer,
                                            ByRef dt As DataTable
                                            ) As Integer

        Dim ret As Integer
        Dim Params As New List(Of IDataParameter)

        Dim Param As IDataParameter
        Param = DatabaseLayer.DatabaseManager.InputParameter("StateId",
                                                        DbType.Int32,
                                                        State)
        Params.Add(Param)
        If PageStart > 0 Then
            Param = DatabaseLayer.DatabaseManager.InputParameter("startRow",
                                                            DbType.Int32,
                                                            PageStart)

            Params.Add(Param)

            Param = DatabaseLayer.DatabaseManager.InputParameter("Rows",
                                                            DbType.Int32,
                                                            Rows)
            Params.Add(Param)

        End If

        ret = DatabaseLayer.DatabaseManager.StoredProc("uspGetRelationships", Params, dt, User.CurrentUser.Id)
        Return ret

    End Function

End Class
